package com.mgface.alg.栈;

/**
 * @author wanyuxiang
 * @version 1.0
 * @project Java_Alg
 * @create 2019-11-04 15:06
 **/
public class Test {
    public static class Lnode<T>{
         T data;//当前数据
         Lnode<T> next;//下一个节点
    }

    public static class Mystack<T>{
        private Lnode<T> head;
        public Mystack(){
            head = new Lnode<>();
            head.next = null;
            head.data = null;
        }
        boolean empty(){
            return head.next==null;
        }
        int size(){
            int sum = 0;
            for (Lnode current = head.next;current!=null;current = current.next){
                sum++;
            }
            return sum;
        }
        void push(T data){
            Lnode node = head.next;
            Lnode<T> v = new Lnode<>();
            v.data = data;
            v.next = node;
            head.next = v;
        }

        T pop(){
            Lnode<T> temp = head.next;
           if (temp!=null){
                head.next = temp.next;
               return temp.data;
           }else {
               System.out.println("栈为空。");
               return null;
           }
        }

        T top(){
            if (head.next!=null){
                return head.next.data;
            }
            System.out.println("栈为空。");
            return null;
        }
    }

    public static void main(String[] args) {
        Mystack<Integer> mystack = new Mystack<>();
        mystack.push(123);
        mystack.push(456);
        System.out.println("栈顶元素："+mystack.top());
        System.out.println("栈大小:"+mystack.size());
        System.out.println(mystack.pop());
        System.out.println(mystack.pop());
        System.out.println("栈大小:"+mystack.size());
    }

}
